package com.example.memoapp.activities;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.memoapp.R;
import com.example.memoapp.database.MemoDatabaseHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class EditMemoActivity extends AppCompatActivity {

    private EditText etTitle, etContent;
    private MemoDatabaseHelper dbHelper;
    private int memoId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_memo);

        etTitle = findViewById(R.id.et_memo_title);
        etContent = findViewById(R.id.et_memo_content);
        Button btnSave = findViewById(R.id.btn_save_memo);
        Button btnDelete = findViewById(R.id.btn_delete_memo);
        etContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

        dbHelper = new MemoDatabaseHelper(this);
        memoId = getIntent().getIntExtra("memoId", -1);

        if (memoId != -1) {
            loadMemoDetails();
        } else {
            finish(); // 如果没有传递memoId，关闭页面
        }

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateMemo();
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.deleteMemo(memoId);
                Toast.makeText(EditMemoActivity.this, "备忘录已删除", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(EditMemoActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    private void loadMemoDetails() {
        Cursor cursor = dbHelper.getMemoById(memoId);
        if (cursor.moveToFirst()) {
            String title = cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_TITLE));
            String content = cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_CONTENT));

            etTitle.setText(title);
            etContent.setText(content);
        }
        cursor.close();
    }

    private void updateMemo() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();

        if (title.isEmpty()) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());

        int rowsAffected = dbHelper.updateMemo(memoId, title, content, date);

        if (rowsAffected > 0) {
            Toast.makeText(this, "备忘录已更新", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(EditMemoActivity.this, MainActivity.class);
            startActivity(intent);
        } else {
            Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
        }
    }
}